package com.farm.kaiping.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.farm.common.constant.RedisKeyConstants;
import com.farm.common.security.SecurityUtils;
import com.farm.kaiping.entity.Building;
import com.farm.kaiping.po.BuildingPO;
import com.farm.kaiping.mapper.BuildingMapper;
import com.farm.kaiping.service.BuildingService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farm.common.util.StringRange;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 厂房管理表(Building)表服务实现类
 *
 * @author makejava
 * @since 2025-01-22 13:57:26
 */
@Service
@Slf4j
@AllArgsConstructor
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements BuildingService {

	private final RedisTemplate<String,Object> redisTemplate;
    
	private LambdaQueryWrapper<Building> generateQueryWrapper(BuildingPO buildingPO){
		LambdaQueryWrapper<Building> queryWrapper = new LambdaQueryWrapper<>();
		// 厂房id
		queryWrapper.eq(ObjectUtil.isNotEmpty(buildingPO.getId()), Building::getId, buildingPO.getId());
		queryWrapper.in(ObjectUtil.isNotEmpty(buildingPO.getIdList()), Building::getId, buildingPO.getIdList());
		// 命名
		queryWrapper.like(ObjectUtil.isNotEmpty(buildingPO.getBuildingName()), Building::getBuildingName, buildingPO.getBuildingName());
		// 占地面积(单位米)
		queryWrapper.eq(ObjectUtil.isNotEmpty(buildingPO.getFloorArea()), Building::getFloorArea, buildingPO.getFloorArea());
		// 圈舍数
		queryWrapper.eq(ObjectUtil.isNotEmpty(buildingPO.getPigpenCount()), Building::getPigpenCount, buildingPO.getPigpenCount());
		// 是否删除
		queryWrapper.eq(ObjectUtil.isNotEmpty(buildingPO.getDelFlag()), Building::getDelFlag, buildingPO.getDelFlag());
        queryWrapper.eq(ObjectUtil.isEmpty(buildingPO.getDelFlag()), Building::getDelFlag, 0);
		// 创建人id
		queryWrapper.eq(ObjectUtil.isNotEmpty(buildingPO.getCreateUserId()), Building::getCreateUserId, buildingPO.getCreateUserId());
		// 创建人
		queryWrapper.like(ObjectUtil.isNotEmpty(buildingPO.getCreateUserName()), Building::getCreateUserName, buildingPO.getCreateUserName());
		// 创建时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(buildingPO.getCreateTime()), Building::getCreateTime, buildingPO.getCreateTime());
		// 更新时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(buildingPO.getUpdateTime()), Building::getUpdateTime, buildingPO.getUpdateTime());
		// 更新人
		queryWrapper.like(ObjectUtil.isNotEmpty(buildingPO.getUpdateUserName()), Building::getUpdateUserName, buildingPO.getUpdateUserName());
		// 更新人id
		queryWrapper.eq(ObjectUtil.isNotEmpty(buildingPO.getUpdateUserId()), Building::getUpdateUserId, buildingPO.getUpdateUserId());
		// 租户id
		queryWrapper.eq(ObjectUtil.isNotEmpty(buildingPO.getTenantId()), Building::getTenantId, buildingPO.getTenantId());
		// 创建时间时间区间查询
		queryWrapper.ge(ObjectUtil.isNotEmpty(StringRange.getOne(buildingPO.getCreateTimeBetween(),0)), Building::getCreateTime,StringRange.getOne(buildingPO.getCreateTimeBetween(),0));
		queryWrapper.le(ObjectUtil.isNotEmpty(StringRange.getOne(buildingPO.getCreateTimeBetween(),1)), Building::getCreateTime,StringRange.getOne(buildingPO.getCreateTimeBetween(),1));
		// 更新时间时间区间查询
		queryWrapper.ge(ObjectUtil.isNotEmpty(StringRange.getOne(buildingPO.getUpdateTimeBetween(),0)), Building::getUpdateTime,StringRange.getOne(buildingPO.getUpdateTimeBetween(),0));
		queryWrapper.le(ObjectUtil.isNotEmpty(StringRange.getOne(buildingPO.getUpdateTimeBetween(),1)), Building::getUpdateTime,StringRange.getOne(buildingPO.getUpdateTimeBetween(),1));

		queryWrapper.orderByDesc(Building::getCreateTime,Building::getId);
		return queryWrapper;
	}

	 /**
	  * @describe 分页查询厂房管理表
	  * @author makejava
	  * @date 2025-01-22 13:57:26
	  */
	@Override
	public Page<Building> getBuildingPage(Page page, BuildingPO buildingPO){
		LambdaQueryWrapper<Building> queryWrapper = this.generateQueryWrapper(buildingPO);
        return this.baseMapper.selectPage(page, queryWrapper);
	}
	
	/**
     * @describe 查询厂房管理表list
     * @author  makejava
     * @date  2025-01-22 13:57:26
     */
	@Override
	public List<Building> getBuildingList(BuildingPO buildingPO){
		LambdaQueryWrapper<Building> queryWrapper = this.generateQueryWrapper(buildingPO);
        return this.baseMapper.selectList(queryWrapper);
	}
	
	/**
     * @describe 获取一条数据 厂房管理表
     * @author  makejava
     * @date  2025-01-22 13:57:26
     */
	@Override
	public Building getBuilding(BuildingPO buildingPO){
		LambdaQueryWrapper<Building> queryWrapper = this.generateQueryWrapper(buildingPO);
        queryWrapper.last("LIMIT 1");
        return this.baseMapper.selectOne(queryWrapper);
	}

    /**
     * @describe 新增厂房管理表
     * @author  makejava
     * @date  2025-01-22 13:57:26
     */
	@Override
	public Integer saveBuilding(BuildingPO buildingPO) {
		if(ObjectUtil.isEmpty(buildingPO.getBuildingName())){
			throw new RuntimeException("命名为空");
		}
		int count = this.count(new LambdaQueryWrapper<Building>().eq(Building::getDelFlag,0).eq(Building::getBuildingName, buildingPO.getBuildingName()));
		if (count>0){
			throw new RuntimeException(buildingPO.getBuildingName()+"已存在");
		}
		buildingPO.setDelFlag(0);
		buildingPO.setTenantId(SecurityUtils.getUser().getTenantId());// 租户ID
		buildingPO.setCreateTime(LocalDateTime.now());
		buildingPO.setCreateUserId(SecurityUtils.getUser().getUserId());// 创建人
		buildingPO.setCreateUserName(SecurityUtils.getUser().getRealName());// 创建人
		buildingPO.setUpdateTime(LocalDateTime.now());
		Building building = BeanUtil.copyProperties(buildingPO, Building.class);
		this.save(building);

		redisTemplate.opsForValue().set(RedisKeyConstants.BUILDING_NAME+building.getId(),building.getBuildingName());

		return building.getId();
	}

	/**
     * @describe 修改厂房管理表
     * @author  makejava
     * @date  2025-01-22 13:57:26
     */
	@Override
	public String updateBuilding(BuildingPO buildingPO) {
		if (ObjectUtil.isEmpty(buildingPO.getId())) {
			throw new RuntimeException("id为空");
		}

		buildingPO.setUpdateUserId(SecurityUtils.getUser().getUserId());// 更新人
		buildingPO.setUpdateUserName(SecurityUtils.getUser().getRealName());// 更新人
		buildingPO.setUpdateTime(LocalDateTime.now());
		Building building = BeanUtil.copyProperties(buildingPO, Building.class);
		this.updateById(building);

		redisTemplate.opsForValue().set(RedisKeyConstants.BUILDING_NAME+building.getId(),building.getBuildingName());

		return "修改成功";
	}

	/**
     * @describe 通过id删除厂房管理表
     * @author  makejava
     * @date  2025-01-22 13:57:26
     */
	@Override
	public String removeBuilding(BuildingPO buildingPO) {
		if (ObjectUtil.isEmpty(buildingPO.getBatchDel())) {
			throw new RuntimeException("删除数据未选择");
		}
		LambdaUpdateWrapper<Building> removeWrapper = new LambdaUpdateWrapper<>();
		removeWrapper.in(Building::getId, buildingPO.getBatchDel());
		removeWrapper.set(Building::getDelFlag,1);
		removeWrapper.set(Building::getUpdateUserId,SecurityUtils.getUser().getUserId());// 更新人
		removeWrapper.set(Building::getUpdateUserName,SecurityUtils.getUser().getRealName());// 更新人
		removeWrapper.set(Building::getUpdateTime,LocalDateTime.now());

		this.update(removeWrapper);

		redisTemplate.delete(buildingPO.getBatchDel().stream().map(x -> RedisKeyConstants.BUILDING_NAME+x).collect(Collectors.toList()));

		return "删除成功";
	}
	
}

